package com.freesid.ask.client;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.freesid.ask.client.field.BaseField;
import com.freesid.ask.client.field.CheckboxField;
import com.freesid.ask.client.field.HeadingField;
import com.freesid.ask.client.field.IntegerField;
import com.freesid.ask.client.field.RadioField;
import com.freesid.ask.client.field.SelectField;
import com.freesid.ask.client.field.TextField;
import com.freesid.ask.client.field.TextareaField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;

/**
 * 
 * @author jkyang
 *
 */
public class FieldFactory {
	
	
	AskConstants cons = GWT.create(AskConstants.class);
	
	protected FieldFactory() {
		
		fieldMapping.put(cons.radio(), new RadioField()); 
		fieldMapping.put(cons.text(), new TextField()) ;
		fieldMapping.put(cons.textArea(), new TextareaField());
		fieldMapping.put(cons.number(), new IntegerField()) ;
		fieldMapping.put(cons.select(), new SelectField()) ;
		fieldMapping.put(cons.checkbox(), new CheckboxField()) ;
		
		fieldMapping.put(cons.heading(), new HeadingField()) ;
		
	}
	
	private Map<String, BaseField> fieldMapping = new LinkedHashMap<String, BaseField>()  ;
	
	private static FieldFactory factory = new FieldFactory() ; 
	
	public static FieldFactory getFactory() {
		return factory ;
	}
	
	public Button getFieldButtonByName(String name) {
		return fieldMapping.get(name).getIcon() ;
	}
	
	public BaseField getFieldByName(String name) {
		if(name.equals(cons.radio())) {
			return new RadioField();
		}
		if(name.equals(cons.text())) {
			return new TextField();
		}
		if(name.equals(cons.textArea())) {
			return new TextareaField();
		}
		if(name.equals(cons.number())) {
			return new IntegerField();
		}
		if(name.equals(cons.select())) {
			return new SelectField();
		}
		if(name.equals(cons.checkbox()) ) {
			return new CheckboxField();
		}
		if(name.equals(cons.heading())) {
			return new HeadingField();
		}
		return null ;
	}
	
	public List<String> getAllFieldNames() {
		List<String> names = new ArrayList<String>();
		names.addAll(fieldMapping.keySet());
		return  names;
	}

	
}
